package com.hui.config;

import com.hui.filter.EncodingFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * WebConfig 类似于web.xml 可以监听tomcat启动然后装载需要的filter和servlet
 */
@Configuration
public class MyWebAppConfig extends WebMvcConfigurerAdapter {

    /**
     * 信息转换器
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
    }

    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则.addPathPatterns("/**")
        // excludePathPatterns 用户排除拦截.excludePathPatterns("/user/deleteUserById")
//        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**").excludePathPatterns("/deleteUserById");
        //registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    /**
     * 自定义web——字符过滤filter
     */
    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new EncodingFilter());
        registration.addInitParameter("encoding", "UTF-8");
        registration.setName("encodingFilter");
        registration.setOrder(0);
        return registration;
    }


}